package com.vcar.LDiceClient.manager
{
	import flash.utils.Dictionary;

	/**
	 * 時間片輪管理器
	 * @author rhj
	 * 为了让flash在播放时很流畅，每帧的处理时间不可以超过(1000/24)毫秒的时间
	 */
	public class TickManager
	{
		private var ticks:Dictionary
		private var frameCount:int=0;

		public function TickManager()
		{
			ticks=new Dictionary();
		}

		/**
		 *每帧的最大处理时间，毫秒单位
		 */
		private var maxTime:Number=50;

		private static var _instance:TickManager;

		public static function get instance():TickManager
		{
			if (_instance == null)
			{
				_instance=new TickManager();
			}
			return _instance;


		}


		public function addTick(t:ITick):void
		{
			if (ticks[t.clipName] != null)
			{
				return;
			}
			ticks[t.clipName]=t;
		}

		public function removeTick(t:ITick):void
		{
			delete ticks[t.clipName];
		}

		/**
		 * 调用接口
		 *
		 */
		public function enterFrame():void
		{
			frameCount++;
			var start:Number=ApplicationFacade.getInstance().getDate().getTime();
			var end:Number;
			for each (var val:ITick in ticks)
			{

				if ((frameCount % val.step) == 0)
				{
					val.doTick();
				}
				end=ApplicationFacade.getInstance().getDate().getTime();
				if ((end - start) > maxTime)
				{
					break;
				}
			}
		}


	}
}